package Controllers;

import java.io.Console;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import Stock.TradingStock;
import Stock.Transaction;
import Stock.StockStatus;
import Stock.Stock;
import Stock.Wallet;

public class WalletController extends Controller {

	private Wallet m_Wallet;
	private Stock m_Stock;
	private List<Transaction> m_Transactions;
	private String m_FilePath;
	private String m_WalletName;
	private Byte m_BaseLineATR=0;

	public WalletController(HttpServletRequest request,
			HttpServletResponse response, String FilePath) {
		super(request, response);

		m_WalletName = request.getParameter("walletName");
		m_FilePath = FilePath;
		 
		try {
			String s = request.getParameter("atrBaseLine");
			System.out.println(s);
			m_BaseLineATR = Byte.parseByte(s);
		} catch (Exception e) {
			m_BaseLineATR = 0;
		}
		
		m_Wallet = new Wallet(m_WalletName);
		m_Wallet.loadWallet(m_FilePath);
		readWalletValues(response);
	}

	private void readWalletValues(HttpServletResponse response) {
		System.out.println("total start value: " + m_Wallet.getStartvalue());

		try {
			m_Transactions = m_Wallet.GetTotalStock();
			m_Stock = new Stock(m_WalletName, m_Transactions,
					StockStatus.Open, m_Wallet.getStartvalue(), new Date());

			String jsonText = setObjectInJson();

			response.setContentType("application/json");
			response.getWriter().print(jsonText);

		} catch (Exception e) {

			e.printStackTrace();
		}

	}

	@SuppressWarnings("unchecked")
	private String setObjectInJson() {
		JSONObject jsonObject = new JSONObject();

		@SuppressWarnings("deprecation")
		String datum = (m_Stock.getStartDate().getYear() + 1900) + "-"
				+ m_Stock.getStartDate().getMonth() + "-"
				+ m_Stock.getStartDate().getDay();
		TradingStock tradingStock =  new TradingStock(m_Stock,m_BaseLineATR);
		Stock stock =tradingStock.getCalculatedStock();

		jsonObject.put("stockName", m_Stock.getStockName());
		jsonObject.put("startValue", m_Stock.getStartValue());
		jsonObject.put("stockAmount", m_Stock.getAmount());
		jsonObject.put("stockStartDate", datum);
		jsonObject.put("stockStatus", m_Stock.getStatus().toString());
		jsonObject.put("Transactions",
				getBasicTransactions(m_Stock.getStockTransactions()));
		jsonObject.put("OHLC", getOHLC(m_Stock.getStockTransactions()));
		jsonObject.put("ATR", getJsonATR());


		jsonObject.put("CalculatedTransactions",getBasicTransactions(stock.getStockTransactions()));
		jsonObject.put("stopDates",  tradingStock.getStopTradingDates());
		jsonObject.put("startDates",  tradingStock.getStartTradingDates());
		 System.out.println(tradingStock.getStartTradingDates());
	//	jsonObject.put("calculatedOHLC",getOHLC(stock.getStockTransactions()));
 
		return jsonObject.toString();
	}

	@SuppressWarnings("unchecked")
	private JSONArray getBasicTransactions(List<Transaction> stockTransactions) {
		JSONArray array = new JSONArray();

		for (Transaction s : stockTransactions) {
			JSONArray arrayStockTransactions = new JSONArray();
			arrayStockTransactions.add(s.getStockDate().getTime());
			arrayStockTransactions.add(s.getAdjustmentClose());
			array.add(arrayStockTransactions);
		}

		return array;
	}

	@SuppressWarnings("unchecked")
	private JSONArray getOHLC(List<Transaction> stockTransactions) {

		JSONArray array = new JSONArray();

		for (Transaction transaction : stockTransactions) {
			JSONArray stockTransactionsArray = new JSONArray();
			stockTransactionsArray.add(transaction.getStockDate().getTime());
			stockTransactionsArray.add(transaction.getOpen());
			stockTransactionsArray.add(transaction.getHigh());
			stockTransactionsArray.add(transaction.getLow());
			stockTransactionsArray.add(transaction.getClose());
			array.add(stockTransactionsArray);
		}

		return array;
	}

	@SuppressWarnings("unchecked")
	private JSONArray getJsonATR() {
		System.out.println("Json ATR");
		JSONArray array = new JSONArray();

		List<Float> f = m_Stock.getATR();

		for (int i = 0; i < m_Transactions.size(); i++) {
			JSONArray a = new JSONArray();
			a.add(m_Transactions.get(i).getStockDate().getTime());
			a.add(f.get(i));
			array.add(a);
		}

		return array;
	}

}
